public class MessaageUtils {


	/**
	 * <P> This method is used to add message to the current Visualforce page, based on the severity and Message text provided.</P>
	 * @Param: messageSeverity is instance of ApexPages.severity,<code> possible values can be.</code>
	 * <UL>
	 *<li>CONFIRM</li>
	 *<li>ERROR</li>
	 *<li>FATAL</li>
	 *<li>INFO</li>
	 *<li>WARNING</li>
	 * </UL>
	 * @param: message as String that message to be added.
	 */
	public static void addMessage(ApexPages.severity messageSeverity, String message) {
		
        ApexPages.addMessage( new ApexPages.Message(messageSeverity,message));
         System.debug('Message := ' + Message+ ' Added with Severity := ' + messageSeverity );
		
	}
	
	/**
	 * <P> This method is used to add List of messages to the current Visualforce page, based on the Severity and Message text provided.</P>
	 * 
	 * @param: message as String that message to be added.
	 */
	public static void addMessages(List<ApexPages.Message> errormessages) {
		if(null!=errormessages){
        
        for(ApexPages.Message msg: errormessages){
        	if(null!=msg ){
        	addMessage(msg.getSeverity(), msg.getSummary());
        	}
        }
         System.debug('Number of Messages added:= ' + errormessages.size());
		}
	}
	
	/**
	 * <P> This method is used to add Error message to the current Visualforce page, based on the Message text provided.</P>
	 * 
	 * @param: message as String that message to be added.
	 */
	public static void addErrorMessage(String message) {
		
        ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR,message));
         System.debug('Added Error Message := ' + Message );
		
	}
	
	/**
	 * <P> This method is used to add Warning message to the current Visualforce page, based on the Message text provided.</P>
	 * 
	 * @param: message as String that message to be added.
	 */
	public static void addWarningMessage(String message) {
		
        ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.WARNING,message));
         System.debug('Added Error Message := ' + Message );
		
	}
	/**
	 * <P> This method is used to add Info message to the current Visualforce page, based on the Message text provided.</P>
	 * 
	 * @param: message as String that message to be added.
	 */
	public static void addInfoMessage(String message) {
		ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.INFO,message);
        ApexPages.addMessage(errormsg);
         System.debug('Added INFO Message := ' + Message );
		
	}
	
	/**
	 * <P> This method is used to add Fatal message to the current Visualforce page, based on the Message text provided.</P>
	 * 
	 * @param: message as String that message to be added.
	 */
	public static void addFatalMessage(String message) {
		ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.FATAL,message);
        ApexPages.addMessage(errormsg);
         System.debug('Added FATAL Message := ' + Message );
		
	}
	
	/**
	 * <P> This method is used to add Confirm message to the current Visualforce page, based on the Message text provided.</P>
	 * 
	 * @param: message as String that message to be added.
	 */
	public static void addConfirmMessage(String message) {
		ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.CONFIRM,message));
         System.debug('Added CONFIRM Message := ' + Message );
		
	}
	
	public static ApexPages.Message[] getMessagesAtLevel(ApexPages.Severity level,
Boolean includeParents){
final ApexPages.Message[] all = ApexPages.getMessages();
final List<ApexPages.Message> filtered = new List<ApexPages.Message>();
if(all != null && all.size() > 0){
for(ApexPages.Message message : all){
if(message.getSeverity() == level
|| (includeParents && isMessageAtLevelOrHigher(message,level))){
filtered.add(message);
}
}
}
return filtered;
}


public static Boolean isMessageAtLevelOrHigher(ApexPages.Message message, ApexPages.Severity level){
if(level == null || message == null){
return false;
} 
ApexPages.Severity msgLevel = message.getSeverity();
while(true){
if(level == msgLevel){
return true;
}
if(msgLevel == ApexPages.Severity.CONFIRM){msgLevel = ApexPages.Severity.WARNING;}
else if(msgLevel == ApexPages.Severity.INFO){msgLevel = ApexPages.Severity.WARNING;}
else if(msgLevel == ApexPages.Severity.WARNING){msgLevel = ApexPages.Severity.ERROR;}
else if(msgLevel == ApexPages.Severity.ERROR){msgLevel = ApexPages.Severity.FATAL;}
else { break; }
}
return false;
}
public static ApexPages.Message[] getErrorMessages(){
return getMessagesAtLevel(ApexPages.Severity.ERROR, true);
}

public static Boolean hasErrorMessages(){
final ApexPages.Message[] errorMessages = getErrorMessages();
return errorMessages != null && errorMessages.size() > 0;
}

public static ApexPages.Message[] getErrorMessagesOnly(){
return getMessagesAtLevel(ApexPages.Severity.ERROR, false);
}

public static Boolean hasErrorMessagesOnly(){
final ApexPages.Message[] errorMessagesOnly = getErrorMessagesOnly();
return errorMessagesOnly != null && errorMessagesOnly.size() > 0;
}

public static String messagesToString(){
return messagesToString(ApexPages.getMessages());
}

public static String messagesToString(ApexPages.Message[] messages){
if(messages == null){
return 'null';
}
if( messages.size() <= 0){
return '{empty}';
}
String returnString = '';
for(Integer i = 0; i < messages.size(); i++){
if(i != 0){ returnString += '; '; }
returnString += 'ApexPages.Message[';
returnString += i;
returnString += ']: (summary=';
returnString += messages[i].getSummary();
returnString += ',componentLabel=';
returnString += messages[i].getComponentLabel();
returnString += ',severity=';
returnString += messages[i].getSeverity();
returnString += ',detail=';
returnString += messages[i].getDetail();
returnString += ')';
}
return '{' + returnString + '}';
}
	
}